﻿#include "CommunicationDialog.h"

CommunicationDialog::CommunicationDialog(QWidget *parent)
	: QDialog(parent)
	, ui(new Ui::CommunicationDialogClass())
{
	ui->setupUi(this);
	init();
	//updatePortInfo();
}

CommunicationDialog::~CommunicationDialog()
{
	delete ui;
	delete m_pCommunicator;
}

void CommunicationDialog::init()
{
	m_pCommunicator = new Communicator();
}

HSS::PortInformation CommunicationDialog::getPortInfo()
{
	HSS::PortInformation portInfo;
	portInfo.portName = ui->serialPortName->currentText();
	switch (ui->baudRateName->currentIndex())
	{
	case 0:
		portInfo.baudRate = QSerialPort::Baud1200;
	case 1:
		portInfo.baudRate = QSerialPort::Baud2400;
	case 2:
		portInfo.baudRate = QSerialPort::Baud4800;
	case 3:
		portInfo.baudRate = QSerialPort::Baud9600;
	case 4:
		portInfo.baudRate = QSerialPort::Baud19200;
	case 5:
		portInfo.baudRate = QSerialPort::Baud38400;
	case 6:
		portInfo.baudRate = QSerialPort::Baud57600;
	case 7:
		portInfo.baudRate = QSerialPort::Baud115200;
	default:
		qDebug() << "BaudRateName Error!!!";
		break;
	}
	switch (ui->comboBox_dataBits->currentIndex())
	{
	case 0:
		portInfo.dataBits = QSerialPort::Data5;
	case 1:
		portInfo.dataBits = QSerialPort::Data6;
	case 2:
		portInfo.dataBits = QSerialPort::Data7;
	case 3:
		portInfo.dataBits = QSerialPort::Data8;
	default:
		qDebug() << "dataBits Error!!!";
		break;
	}
	switch (ui->comboBox_parity->currentIndex())
	{
	case 0:
		portInfo.parity = QSerialPort::NoParity;
	case 1:
		portInfo.parity = QSerialPort::EvenParity;
	case 2:
		portInfo.parity = QSerialPort::OddParity;
	case 3:
		portInfo.parity = QSerialPort::SpaceParity;
	case 4:
		portInfo.parity = QSerialPort::MarkParity;
	default:
		qDebug() << "parity Error!!!";
		break;
	}
	switch (ui->comboBox_stopBits->currentIndex())
	{
	case 0:
		portInfo.stopBits = QSerialPort::OneStop;
	case 1:
		portInfo.stopBits = QSerialPort::OneAndHalfStop;
	case 2:
		portInfo.stopBits = QSerialPort::TwoStop;

	default:
		qDebug() << "StopBits Error!!!";
		break;
	}
	return portInfo;
}

void CommunicationDialog::on_pushButton_set_clicked()
{
	emit sendPortInfo(getPortInfo());
}

